<?php
/**
* Plugin management.
*
* Copyright (c) 2009 James Gauld <james@jamesgauld.net>
* This file is part of Scribble.
* @license http://www.scribblecms.co.uk/license.txt
*
* @package Scribble
*/
use Buan\Config;
class ScribblePluginManager {

	/**
	* Registered plugins.
	*
	* @var array
	*/
	static private $plugins = array();

	/**
	* Register a plugin for use with the application.
	*
	* @param string Absolute path to the plugin class
	* @param string The URL by which this plugin can be accessed (optional)
	* @return void
	*/
	static public function register($classPath, $urlRoot='') {
		$p = new StdClass();
		$p->classPath = $classPath;
		$p->className = substr(basename($classPath), 0, -4);
		$p->instance = NULL;
		self::$plugins[$p->className] = $p;
		Config::set("ext.Scribble.plugin.{$p->className}", array(
			'urlRoot'=>$urlRoot
		));
	}

	/**
	* Return the singleton instance of the specified plugin class.
	*
	* @class string Plugin name
	* @return ScribblePlugin
	*/
	static public function get($plugin) {
		if(isset(self::$plugins[$plugin])) {
			$p = self::$plugins[$plugin];
			if($p->instance===NULL) {
				include($p->classPath);
				return $p->instance = new $p->className();
			}
			return $p->instance;
		}
		else {
			return NULL;
		}
	}

	/**
	* Return instances of ALL registered plugins.
	*
	* @return array
	*/
	static public function getAll() {
		$plugins = array();
		foreach(self::$plugins as $k=>$p) {
			$plugins[] = self::get($k);
		}
		return $plugins;
	}
}
?>